package com.yzc.android;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Process;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.Configuration;
import com.baidu.mapapi.SDKInitializer;
import com.yzc.android.beans.BeaconBean;
import com.yzc.android.beans.MotionDeltaBean;
import com.yzc.android.common.app.AppConfigManager;
import com.yzc.android.event.BeaconScanEvent;
import com.yzc.android.event.EventBusManager;
import com.yzc.android.manager.sensor.SensorConstant;
import com.yzc.android.service.AlgorithmicAliveService;
import com.yzc.android.utils.LogUtils;
import com.yzc.android.utils.ToastUtil;
import de.greenrobot.event.EventBus;

/**
 * Created by yzc on 15/9/18.
 */
public class KDApp extends Application {

    private static KDApp mInstance;
    public static KDApp getInstance() {
        return mInstance;
    }

    private BeaconBean mBeacon;

    public static final String mBroadcastSensorAction = "broadcast.sensor";

    public static final String mBroadcastIBeaconEnterAction = "broadcast.ibeacon.enter";

    public static final String mBroadcastIBeaconExitAction = "broadcast.ibeacon.exit";

    private IntentFilter mIntentFilter;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(mBroadcastSensorAction)) {
                MotionDeltaBean deltaBean = intent.getParcelableExtra("Data");
                double acceleration = Math.abs(deltaBean.accelerationDelta - SensorConstant.kAccelerationThreshold);
                double yaw = Math.abs(deltaBean.yawDelta - SensorConstant.kYawThreshold);
                if (Double.compare(yaw, acceleration) > 0) {
                    if (Double.compare(deltaBean.yawDelta, SensorConstant.kYawThreshold) > 0) {
                        //LogUtils.local(SensorConstant.kAccelerometerLogTag, "急转弯阈值(%1s),当前yawDelta(%1s),应用提醒急转弯",
                        //               SensorConstant.kYawThreshold, deltaBean.yawDelta);
                        //ToastUtil.getInstance().showToast("应用察觉到您刚才做了急转弯，请注意驾驶安全");
                    }
                    //                        else if (Double.compare(deltaBean.accelerationDelta, SensorConstant
                    // .kAccelerationThreshold) > 0) {
                    //                            LogUtils.local(SensorConstant.kAccelerometerLogTag, "急加（减）速阈值(%1s),
                    // 当前accelerationDelta值(%2s),应用提醒急加（减）速",
                    //                                    SensorConstant.kAccelerationThreshold, deltaBean
                    // .accelerationDelta);
                    //                            ToastUtil.getInstance().showToast("应用察觉到您刚才做了急加（减）速，请注意驾驶安全");
                    //                        }
                } else {
                    if (Double.compare(deltaBean.accelerationDelta, SensorConstant.kAccelerationThreshold) > 0) {
                        //LogUtils.local(SensorConstant.kAccelerometerLogTag,
                        //               "急加（减）速阈值(%1s),当前accelerationDelta值(%2s),应用提醒急加（减）速",
                        //               SensorConstant.kAccelerationThreshold, deltaBean.accelerationDelta);
                        //ToastUtil.getInstance().showToast("应用察觉到您刚才做了急加（减）速，请注意驾驶安全");
                    }
                    //                        else if (Double.compare(deltaBean.yawDelta, SensorConstant
                    // .kYawThreshold) > 0) {
                    //                            LogUtils.local(SensorConstant.kAccelerometerLogTag, "急转弯阈值(%1s),当前yawDelta
                    // (%2s),应用提醒急转弯", SensorConstant.kYawThreshold, deltaBean.yawDelta);
                    //                            ToastUtil.getInstance().showToast("应用察觉到您刚才做了急转弯，请注意驾驶安全");
                    //                        }
                }
            } else if (intent.getAction().equals(mBroadcastIBeaconEnterAction)) {
                mBeacon = intent.getParcelableExtra("Data");
                EventBusManager.getInstance().post(new BeaconScanEvent(mBeacon));
            }
            else if (intent.getAction().equals(mBroadcastIBeaconExitAction)) {
                EventBusManager.getInstance().post(new BeaconScanEvent(null));
            }
        }
    };

    public BeaconBean getBeacon() {
        return mBeacon;
    }


    private boolean mInitialized = false;

    private EventBus mEventBus = new EventBus();
    public EventBus getEventBus() {
        return mEventBus;
    }

    private AppConfigManager mAppConfigMgr;
    public AppConfigManager getAppConfigManager() {
        if (mAppConfigMgr == null)
        {
            mAppConfigMgr = new AppConfigManager();
        }
        return mAppConfigMgr;
    }

    public static Context getContext() {
        return getInstance().getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        ToastUtil.getInstance().init(this);
    }

    public synchronized void init() {
        if (!mInitialized) {
            mInitialized = true;
            LogUtils.d(AlgorithmicAliveService.ALIVE_SERVICE_TAG, "application(%s) onCreate", Process.myPid());

            //NetworkStatusManager.init(this);
            SDKInitializer.initialize(getApplicationContext());

            Configuration dbConfiguration = new Configuration.Builder(this).setDatabaseName("kd.db").create();
            ActiveAndroid.initialize(dbConfiguration);

            mIntentFilter = new IntentFilter();
            mIntentFilter.addAction(mBroadcastSensorAction);
            mIntentFilter.addAction(mBroadcastIBeaconEnterAction);
            mIntentFilter.addAction(mBroadcastIBeaconExitAction);

            registerReceiver(mReceiver, mIntentFilter);

            Intent grayIntent = new Intent(getApplicationContext(), AlgorithmicAliveService.class);
            startService(grayIntent);
        }
    }
}
